package com.glorysoft.hlgdpda.ui.main.viewmodel

import android.app.Activity
import androidx.databinding.ObservableArrayList
import com.glorysoft.hlgdpda.BR
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseViewModel
import com.glorysoft.hlgdpda.base.Constant
import com.glorysoft.hlgdpda.entity.ui.CallObserveBean
import com.glorysoft.hlgdpda.utils.MyUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils
import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.io.InputStream

/**
 * FileName: MainViewModel
 * Author: XJH
 * Date: 2021/5/13 17:36
 * Description:
 * History:
 */
class MainViewModel : BaseViewModel() {

    var checkItems = ObservableArrayList<FreeUI_TabIndexGridEntity>()
    var mfgItems = ObservableArrayList<FreeUI_TabIndexGridEntity>()
    val START_ACTIVITY = 0
    val MFGLAYOUT = 1
    val CHECKLAYOUT = 2

    private val mainItemClick = object : OnItemClickListener {
        override fun onItemClick(item: Any) {
            defUI.callObserve.value = CallObserveBean(START_ACTIVITY, item)
        }
    }

    var itemBinding = ItemBinding.of<FreeUI_TabIndexGridEntity>(BR.tabIndexGrid, R.layout.item_main)
            .bindExtra(BR.itemClickListener, mainItemClick)

    fun initAssets(context : Activity){
        val mfgJsonStream: InputStream = context.resources.assets.open("mfg.txt")
        val mfgJson = FreeApi_StringUtils.getString(mfgJsonStream)
        val mfgEntity = filteringData(
            Gson().fromJson(
                mfgJson,
                object : TypeToken<MutableList<FreeUI_TabIndexGridEntity?>?>() {}.type
            )
        )

        if (mfgEntity.isEmpty()) {
            defUI.callObserve.value = CallObserveBean(MFGLAYOUT, "")
        }else{
            mfgItems.addAll(MyUtils.setPicToDrawable(context,mfgEntity))
        }


        val checkJsonStream: InputStream = context.resources.assets.open("check.txt")
        val checkJson = FreeApi_StringUtils.getString(checkJsonStream)
        val checkEntity :List<FreeUI_TabIndexGridEntity> = Gson().fromJson(checkJson, object : TypeToken<List<FreeUI_TabIndexGridEntity?>?>() {}.type)
        if (checkEntity.isEmpty()){
            defUI.callObserve.value = CallObserveBean(CHECKLAYOUT, "")
        }else{
            checkItems.addAll(MyUtils.setPicToDrawable(context,checkEntity))
        }
    }

    private fun filteringData(entity: MutableList<FreeUI_TabIndexGridEntity>): List<FreeUI_TabIndexGridEntity> {
        val it: MutableIterator<FreeUI_TabIndexGridEntity> = entity.iterator()
        while (it.hasNext()) {
            val item = it.next()
            try {
                val aClass = Class.forName(item.skipClass)
                val name = aClass.simpleName
                if (!Constant.AUTHORITYLIST.contains(item.text)) {
                    it.remove()
                }
            } catch (e: ClassNotFoundException) {
                e.printStackTrace()
            }
        }
        return entity
    }


    interface OnItemClickListener {
        fun onItemClick(item: Any)
    }
}